import Joi from 'joi';
import {Context} from 'koa';
import {SERVER_ADDR} from '@/config';
import {validateRequestFile} from '@/utils/ValidateDecorator';
import {WrapperResponse} from '@/utils/wrapperResponse';

class ImageController {
    @validateRequestFile(Joi.object().keys({
        file: Joi.alternatives(Joi.object(), Joi.array()).required()
    }))
    async compressImg(ctx: Context) {
        const {file} = ctx.request.files!;
        const files = Array.isArray(file) ? file : [file];
        const res = await Promise.all(files.map(async item => {
            const {newFilename} = item;
            return `${SERVER_ADDR}/tmp/${newFilename}`;
        }));
        ctx.body = WrapperResponse.success(res);
    }
}

export default new ImageController();
